//
//  GKZYMyInfoViewController.m
//  GKZY_CLASS
//
//  Created by chenziqiang on 15/12/5.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "GKZYMyInfoViewController.h"
#import "GKZYMyInfoDetailViewController.h"
#import "GKZYMyInfoFeedbackViewController.h"
#import "GKZYMyInfoSettingViewController.h"
#import "GKZYLoginUserInfoEntity.h"
#import "GKZYMyInfoHeaderCell.h"
#import "GKZYMyInfoModel.h"
#import "GKZYMyInfoCell.h"
#import "GKZY_CLASS.h"
#import "ResourceHelper.h"
#import "FontImageHelper.h"
#import "ClassHelper.h"
#import <Masonry/Masonry.h>



@interface GKZYMyInfoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *mainTableView;
@property (nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,strong) NSMutableArray *cells;
@end

@implementation GKZYMyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我";
    //原生导航栏的状态栏颜色改变
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
    [self.view addSubview:self.mainTableView];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    [self initialData];
}

/**
 * 初始化数据
 */
- (void)initialData{
    UIColor *leftIconColor = [UIColor colorWithRed:49/255.0 green:145/255.0 blue:254/255.0 alpha:1];
    NSString *fontName = @"xxpticons";
    int sizeForLeftIcon = 34;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSData *currentStudentInfoData = [userDefaults valueForKey:OO_USER_INFO_KEY];
    GKZYLoginUserInfoEntity *userInfoEntity = [NSKeyedUnarchiver unarchiveObjectWithData:currentStudentInfoData];
    
    NSMutableArray *dataArray = [NSMutableArray array];
    NSMutableArray *row1 = [NSMutableArray array];
    GKZYMyInfoModel *model11 = [GKZYMyInfoModel new];
    model11.imgName = @"headNew";
    model11.imgSuffix = @"png";
    if (userInfoEntity.header && ![userInfoEntity.header isKindOfClass:[NSNull class]]) {
        model11.img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userInfoEntity.header]]];
    }
    model11.title = userInfoEntity.name;
    model11.subTitle = @"";
    model11.showArrows = YES;
    model11.isHeader = YES;
    model11.forwardToInfo = YES;
    model11.isDetail = NO;
    [row1 addObject:model11];
    [dataArray addObject:row1];
    
    /*
    NSMutableArray *row2 = [NSMutableArray array];
    OOMyInfoModel *model21 = [OOMyInfoModel new];
    model21.img = [FontImageHelper imageWithIcon:@"J" fontName:fontName iconColor:leftIconColor iconSize:sizeForLeftIcon];
    model21.title = @"我的主页";
    model21.showArrows = YES;
    model21.isHeader = NO;
    model21.isDetail = NO;
    [row2 addObject:model21];
    
    OOMyInfoModel *model22 = [OOMyInfoModel new];
    model22.img = [FontImageHelper imageWithIcon:@"m" fontName:fontName iconColor:leftIconColor iconSize:sizeForLeftIcon];
    model22.title = @"分期缴费";
    model22.showArrows = YES;
    model22.isHeader = NO;
    model22.isDetail = NO;
    [row2 addObject:model22];
    [dataArray addObject:row2];
    */
    
    /*
    NSMutableArray *row3 = [NSMutableArray array];
    OOMyInfoModel *model31 = [OOMyInfoModel new];
    model31.img = [FontImageHelper imageWithIcon:@"K" fontName:fontName iconColor:leftIconColor iconSize:sizeForLeftIcon];
    model31.title = @"离线课程";
    model31.showArrows = YES;
    model31.isHeader = NO;
    model31.isDetail = NO;
    [row3 addObject:model31];
    [dataArray addObject:row3];
    */
    
    /*
    NSMutableArray *row4 = [NSMutableArray array];
    OOMyInfoModel *model41 = [OOMyInfoModel new];
    model41.img = [FontImageHelper imageWithIcon:@"j" fontName:fontName iconColor:leftIconColor iconSize:sizeForLeftIcon];
    model41.title = @"我的实名动态、同学互助";
    model41.showArrows = YES;
    model41.isHeader = NO;
    model41.isDetail = NO;
    [row4 addObject:model41];
    
    OOMyInfoModel *model42 = [OOMyInfoModel new];
    model42.img = [FontImageHelper imageWithIcon:@"M" fontName:fontName iconColor:leftIconColor iconSize:sizeForLeftIcon];
    model42.title = @"我的收藏";
    model42.showArrows = YES;
    model42.isHeader = NO;
    model42.isDetail = NO;
    [row4 addObject:model42];
    [dataArray addObject:row4];
    */
    
    NSMutableArray *row5 = [NSMutableArray array];
    GKZYMyInfoModel *model51 = [GKZYMyInfoModel new];
    model51.img = [FontImageHelper imageWithIcon:@"N" fontName:fontName iconColor:leftIconColor iconSize:sizeForLeftIcon];
    model51.title = @"设置";
    model51.showArrows = YES;
    model51.isHeader = NO;
    model51.isDetail = NO;
    model51.forwardToSetting = YES;
    [row5 addObject:model51];
    [dataArray addObject:row5];
    self.datas = dataArray;
}

#pragma mark - UITableView DataSourse
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *row = self.datas[section];
    return row.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GKZYMyInfoModel *model = [self.datas[indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 0 && indexPath.row == 0) {
        NSString *cellName = @"header";
        GKZYMyInfoHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (!cell) {
            cell = [[GKZYMyInfoHeaderCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
        }
        [self.cells addObject:cell];
        cell.hiddenTopLine = indexPath.row != 0 && !model.isHeader;
        cell.data = model;
        return cell;
    }else{
        NSString *cellName = @"myInfo";
        GKZYMyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (!cell) {
            
            cell = [[GKZYMyInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
        }
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.hiddenTopLine = indexPath.row != 0 && !model.isHeader;
        
        [self.cells addObject:cell];
        cell.data = model;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 14;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 70;
    }else{
        return 49;
    }
}

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GKZYMyInfoModel *model = [self.datas[indexPath.section] objectAtIndex:indexPath.row];
    
    UIViewController *vc;
    if (model.forwardToInfo) {
        vc = [[GKZYMyInfoDetailViewController alloc]init];
    }else if (model.forwardToSetting){
        vc = [[GKZYMyInfoSettingViewController alloc]init];
    }
    if (vc) {
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Property Setter

- (void)setDatas:(NSMutableArray *)datas{
    _datas = datas;
    if (datas) {
        [self.mainTableView reloadData];
    }
}

#pragma mark - Getter For Views

-(UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = ({
            UITableView *myInfoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-40) style:UITableViewStyleGrouped];
            myInfoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            myInfoTableView.dataSource = self;
            myInfoTableView.delegate = self;
            myInfoTableView.scrollEnabled = NO;
            myInfoTableView;
        });
    }
    return _mainTableView;
}

@end